package com.yanqu.road.server.manger.activity.lanruosi;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.config.LanRuoSiMonsterInfo;
import com.yanqu.road.entity.activity.lanruosi.config.LanRuoSiNpcInfo;
import com.yanqu.road.entity.activity.tripletown.Monster;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.LanRuoSiBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lanruosi.LanRuoSiModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class LanRuoSiMgr extends TempMgr {

    private static LanRuoSiConfig config;

    public static final int MAX_WIDTH = 6;
    public static final int MAX_HEIGHT = 7;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {


        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    public static void reloadActivityData() {
        getLogger().info("reload LanRuoSi Activity start");

        ActivityInfo activityInfo;

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.LanRuoSiActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no LanRuoSi Activity in show time");
            config = null;
            return;
        }else {
            activityInfo = openActivityInfoList.get(0);
        }

        if(activityInfo != null){
            LanRuoSiConfig tmpConfig = new LanRuoSiConfig();
            tmpConfig.setActivityInfo(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();

            // 配置表配置
            tmpConfig.setBlockConfigMap(LanRuoSiBussiness.getLanRuoSiBlockConfigInfo(tmpActivityId));
            tmpConfig.setScoreConfigMap(LanRuoSiBussiness.getLanRuoSiScoreConfigInfo(tmpActivityId));

            // npc
            Map<Integer, LanRuoSiNpcInfo> tempNpcMap = LanRuoSiBussiness.getLanRuoSiNpcInfoList(tmpActivityId);
            Map<Integer, LanRuoSiMonsterInfo>  tempMonsterInfoMap = LanRuoSiBussiness.getLanRuoSiMonsterListMap(tmpActivityId);
            //解析每一波
            for (Map.Entry<Integer, LanRuoSiMonsterInfo> monsterInfoEntry : tempMonsterInfoMap.entrySet()) {
                LanRuoSiMonsterInfo monsterInfo = monsterInfoEntry.getValue();
                List<String> monsterParamList = StringUtils.stringToStringList(monsterInfo.getNpcStation(), "\\|");
                for (String s : monsterParamList) {
                    List<Integer> monsterParam = StringUtils.stringToIntegerList(s, ",");
                    int id = monsterParam.get(0);
                    LanRuoSiNpcInfo npcInfo = tempNpcMap.get(id);
                    int x = monsterParam.get(1);
                    int y = monsterParam.get(2);
                    Monster monster = new Monster();
                    monster.setId(id);
                    monster.setHp(npcInfo.getNpcHp());
                    monster.setBlood(npcInfo.getNpcHp());
                    monster.setX(x);
                    monster.setY(y);
                    for (int i = 0; i < npcInfo.getPicSize(); i++) {
                        monster.getCols().add(x + i);
                    }
                    monsterInfo.getMonsterList().add(monster);
                }
            }

            tmpConfig.setWaveMonsterMap(tempMonsterInfoMap);
            tmpConfig.setMonsterInfoMap(tempNpcMap);

            // config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.LANRUOSI_SCORE_ITEM_ID.getName()).getIntValue());
            tmpConfig.setEnergyRecItemId(activityConfigMap.get(eActivityConfigType.LANRUOSI_ENERGY_ITEM_ID.getName()).getIntValue());
            List<Integer> energyParam = activityConfigMap.get(eActivityConfigType.LANRUOSI_ENERGY_PARAM.getName()).getIntListValue(";");
            tmpConfig.setMaxEnergy(energyParam.get(0));
            tmpConfig.setEnergyRecTime(energyParam.get(1) * 1000L);
            tmpConfig.setEnergyRecCount(energyParam.get(2));
            tmpConfig.setBlockWeightList(activityConfigMap.get(eActivityConfigType.LANRUOSI_CREATE_BLOCK_WEIGHT_LIST.getName()).getIntListValue(";"));
            tmpConfig.setEliminateScoreList(activityConfigMap.get(eActivityConfigType.LANRUOSI_ELIMINATE_SCORE_PRAM.getName()).getIntListValue(";"));
            tmpConfig.setEliminateDamageList(activityConfigMap.get(eActivityConfigType.LANRUOSI_ELIMINATE_DAMAGE.getName()).getIntListValue(";"));
            tmpConfig.setMultiUnlockScore(activityConfigMap.get(eActivityConfigType.LANRUOSI_MULTI_UNLOCK_SCORE.getName()).getIntValue());
            tmpConfig.setAttackScoreList(activityConfigMap.get(eActivityConfigType.LANRUOSI_ELIMINATE_NPC_SCORE_PRAM.getName()).getIntListValue(";"));

            List<Integer> initBlock = activityConfigMap.get(eActivityConfigType.LANRUOSI_INIT_BLOCK.getName()).getIntListValue(",");
            int[] tmpBlockList = initBlock.stream().mapToInt(item -> item).toArray();
            tmpConfig.setInitBlock(tmpBlockList);
            List<Integer> initBlockMulti = activityConfigMap.get(eActivityConfigType.LANRUOSI_INIT_BLOCK_MULTI.getName()).getIntListValue(",");
            int[] tmpBlockListMulti = initBlockMulti.stream().mapToInt(item -> item).toArray();
            tmpConfig.setInitBlockMulti(tmpBlockListMulti);
            tmpConfig.setMultiUnlockVipLevel(activityConfigMap.get(eActivityConfigType.LANRUOSI_MULTI_UNLOCK_VIP_LEVEL.getName()).getIntValue());
            tmpConfig.setCostEnergyList(activityConfigMap.get(eActivityConfigType.LANRUOSI_COST_ENERGY_PARAM.getName()).getIntListValue(";"));

            if (activityConfigMap.containsKey(eActivityConfigType.LANRUOSI_FAST_UNLOCK_VIP_LEVEL.getName())) {
                tmpConfig.setFastUnlockVipLevel(activityConfigMap.get(eActivityConfigType.LANRUOSI_FAST_UNLOCK_VIP_LEVEL.getName()).getIntValue());
                List<Integer> initBlockFast = activityConfigMap.get(eActivityConfigType.LANRUOSI_INIT_BLOCK_FAST.getName()).getIntListValue(",");
                int[] tmpBlockListFast = initBlockFast.stream().mapToInt(item -> item).toArray();
                tmpConfig.setInitBlockFast(tmpBlockListFast);
            } else {
                // 旧活动没配置不让用
                tmpConfig.setFastUnlockVipLevel(Integer.MAX_VALUE);
                tmpConfig.setInitBlockFast(new int[42]);
            }

            config = tmpConfig;
        }

        getLogger().info("reload LanRuoSi Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            LanRuoSiModule module = gamePlayer.getModule(LanRuoSiModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }

    /**
     * 获取下一波妖怪
     */
    public static int getNextWaveMonsterList(int waveNum, List<Monster> newList) {
        if(config == null){
           return waveNum;
        }
        Map<Integer, LanRuoSiMonsterInfo> map = config.getWaveMonsterMap();
        if(waveNum + 1 <= map.size()){
            for (Monster monster : map.get(waveNum + 1).getMonsterList()) {
                newList.add(monster.clone());
            }
            return waveNum + 1;
        }else {
            for (Monster monster : map.get(2).getMonsterList()) {
                newList.add(monster.clone());
            }
            return 2;
        }
    }

    public static LanRuoSiConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        return config == null ? null : config.getActivityInfo();
    }
}
